//
//  ZJKHomeViewController.swift
//  MicroBlog
//
//  Created by VAD on 16/4/16.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

class ZJKHomeViewController: ZJKVisitorViewController {
    
    let ZJKStatusCellID = "ZJKStatusCellID"
    
//    var statusArray: [ZJKStatus]?
    
    private lazy var statusListViewModel: ZJKStatusListViewModel = ZJKStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        if !userlogin{
            
            visitorView.setVisitorInfo(nil, message: nil)
            
            return
        }
        
        setupUI()
        
        loadData()
    }

    //设置UI
    private func setupUI(){
        
        tableView.registerClass(ZJKStatusCell.self, forCellReuseIdentifier: ZJKStatusCellID)
        
        
        //设置自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置预估行高
        tableView.estimatedRowHeight = 250
        
        //隐藏tableview的分割线
        tableView.separatorStyle = .None
        
        tableView.backgroundColor = UIColor(white: 237/255, alpha: 1)
//        tableView.backgroundColor = UIColor.redColor()
        tableView.tableFooterView = pullupView
        
        tableView.addSubview(zjkRefreshControl)
        
        zjkRefreshControl.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
    }
    //加载数据
    @objc private func loadData(){
        
        statusListViewModel.loadData(pullupView.isAnimating()) { (isSucess) -> () in
            if isSucess {
                //刷新数据
                self.tableView.reloadData()
            }else{
                print("请求数据失败")
            }
        }
        endRefresh()
    }
    
    //停止菊花转
    private func endRefresh(){
        pullupView.stopAnimating()
//        self.refreshControl?.endRefreshing()
        zjkRefreshControl.endRefresh()
    }
    //懒加载控件
    private lazy var pullupView: UIActivityIndicatorView = {
        
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        indicatorView.color = UIColor.orangeColor()
        
        //测试代码
        indicatorView.hidesWhenStopped = false
        
        return indicatorView
        
    }()

    private lazy var zjkRefreshControl: ZJKRefreshControl = ZJKRefreshControl()
}

extension ZJKHomeViewController{
    
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        // 判断是否是将要加载最后一个
        if indexPath.row == statusListViewModel.statusArray!.count - 1 && !pullupView.isAnimating() {
            print("将要加载最后一个cell")
            // 1. 菊花转
            pullupView.startAnimating()
            // 2. 加载数据
            loadData()
        }
        
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statusListViewModel.statusArray?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(ZJKStatusCellID, forIndexPath: indexPath) as! ZJKStatusCell
        
        cell.statusViewModel = statusListViewModel.statusArray![indexPath.row]
        
        return cell
    }
}
